$(function () {
	$("#saveForBt").bind('click',function(){
		submitCallPass();
	});
	yanzheng($("#myPhone"),$("#code"));
});
function checkStrLength(userPass, min, max) {// 验证密码
	if (userPass.length < min || userPass.length > max) {
		easyAlert("输入字符长度须在" + min + "-" + max + "之间!");
		return false;
	} else {
		return true;
	}
}
function submitCallPass() {
	var jsonParam=[];
	if(localStorage.getItem("phoneNoUUID") == null || localStorage.getItem("phoneNoUUID") == ''){
		alert("验证码不正确");
		return;
	}
	jsonParam.push({name:"codeUuid",value:localStorage.getItem("phoneNoUUID")});
	if($("#myPhone").val().trim()!=''){
		jsonParam.push({name:"username",value:$("#myPhone").val()});
	}else{
		alert('手机号不可为空');return;
	}
	if($("#resetPass").val().trim()!='' && checkStrLength($("#resetPass").val(),6,16)){
		jsonParam.push({name:"newpasswd",value:$("#resetPass").val()});
	}else{
		return;
	}
	if($("#codeMsg").val().trim()!=''){
		jsonParam.push({name:"code",value:$("#codeMsg").val()});
	}else{
		alert('验证码不可为空');return;
	}
	var resetPass2 = $("#resetPass2").val();
	if(resetPass2 != $("#resetPass").val()){
		easyAlert("两次输入密码不一致")
		return;
	}
	$.ajax({
		type:'post',
		url:server_url+"rest/frontUser/forgetPass.do?ran="+Math.random(),
		dataType:'json',
		data:jsonParam,
		async : false,
		error:function(){
			alert('网络异常'); 
		},
		success:function(data){
			if(data.retCode != '0000'){
				alert(data.retMsg);
				return;
			}
			easyAlert("保存成功.");
			window.location.href=server_url+"login.do?supply="+params()['supply'];
		}
	});
}
var nums = 60;
function yanzheng(pNumber,code){ //验证码验证
	var conOff = false;
	var clock = '';
	
	pNumber.bind('input propertychange',function(){
		if( $(this).val().length == 11 ){
			if (isPhone($(this).val())) {
				if(nums == 60 || nums==0){
					code.css({
						background:"#0DB664",
						color:"#fff"
					});
					conOff = true;
				}
				
			}else{
				easyAlert("请输入有效的手机号.");
				code.css({
					background:"#fff",
					color:"#eaeaea"
				});
			}
		}else{
			code.css({
				background:"#fff",
				color:"#eaeaea"
			});
		};
	})
	code.click(function(){
		if( conOff == true){
			checkPhone(code);
			/*sendCode(code);*/
		}
	});
	
	function checkPhone(){
		$.ajax({
			type:'post',
			url:server_url+'/rest/supply/register/getUser.do?mobile='+pNumber.val(),
			dataType:'json',
			data:{ran:Math.random()},
			async : false,
			error:function(){
				alert('网络异常'); 
				conOff = true;
				code.css({
					background:"#0DB664",
					color:"#fff"
				});
			},
			success:function(data){
				//return;
				if(data.retCode == "0000"){
					alert('手机号不存在或输入错误');
				}else{
					sendCode(code);
				}
			}
		});
	}
	
	
	
	function sendCode(code){//倒计时
		conOff = false; //将按钮置为不可点击
		code.css({
			background:"#fff",
			color:"#eaeaea"
		});
		code.html(nums+'秒后重新获取');
		clock = setInterval(doLoop, 1000); //一秒执行一次
		$.ajax({
			type:'post',
			url:server_url+'rest/verify/sendCode.do?phone='+pNumber.val(),
			dataType:'json',
			data:{ran:Math.random()},
			async : false,
			error:function(){
				alert('网络异常'); 
				conOff = true;
				code.css({
					background:"#0DB664",
					color:"#fff"
				});
			},
			success:function(data){
				if(localStorage && data.data.codeUuid){
					localStorage.setItem("phoneNoUUID",data.data.codeUuid);
					alert('短信验证码发送成功,请注意查收');
				}else{
					alert("短信发送失败");
				}
				
			}
		});
		
	};
	function doLoop(){//倒计时定时器
		nums--;
		if(nums > 0){
			code.html(nums+'秒后重新获取');
		}
		else{
		  	clearInterval(clock); //清除js定时器
		  	code.html('重新发送验证码');
		  	code.css({
				background:"#0DB664",
				color:"#fff"
			});
		  	nums = 60; //重置时间
		  	conOff = true;
		};
	};
}